import { AjaxService, Req, Res } from "./ajax.service"
import { Injectable } from '@angular/core'

import { User } from './user'

@Injectable()
export class UserService {
  private bh = AjaxService.APIROOT + '/users'
  public static MINE: User
  public static MEMBERS: Array<User>

  login(account, passwd): Promise<Res> {
    var argv = { account: account, password: passwd }
    return AjaxService.request(new Req(`${this.bh}/login.json`, "POST", argv))
  }

  getUserMine(): Promise<User> {
    var argv = { token: localStorage.getItem('token') }
    return new Promise<User>(resolve => {
      if (UserService.MINE)
        resolve()
      else
        AjaxService.request(new Req(`${this.bh}/mine.json`, "GET", argv))
          .then(res => {
            if (!res.error)
              UserService.MINE = res.data
            resolve()
          }).catch((err: any) => { console.log("get user mine error", err) })
      }).then(() => UserService.MINE)
  }

  get(user_id): Promise<User> {
    var argv = { token: localStorage.getItem('token') }
    var user: User
    return new Promise<User>(resolve => {
        AjaxService.request(new Req(`${this.bh}/${user_id}.json`, "GET", argv))
          .then(res => {
            if (!res.error)
              user = res.data
            resolve()
          }).catch((err: any) => { console.log("get user error", err) })
      }).then(() => user)
  }

  getList(): Promise<Array<User>> {
    var argv = { token: localStorage.getItem('token') }
    return new Promise<Array<User>>(resolve => {
      if (UserService.MEMBERS)
        resolve()
      else
        AjaxService.request(new Req(`${this.bh}.json`, "GET", argv))
          .then(res => {
            if (!res.error)
              UserService.MEMBERS = res.data
            else if (res.error == "TOKEN-ERROR")
              alert("需要登录")
            resolve()
          }).catch((err: any) => { console.log("get users error", err) })
      }).then(() => UserService.MEMBERS)
  }


  list(): Promise<Array<User>> {
    var argv = new Array()
    if (localStorage.getItem("token"))
      argv["token"] = localStorage.getItem("token")
    var tasks: Array<User> = [ ]
      return new Promise<Array<User>>(resolve => {
        AjaxService.request(new Req(`${this.bh}/list.json`, "GET", argv))
          .then(res => {
            if (!res.error)
              tasks = res.data
            resolve()
          }).catch((err: any) => { console.log("get tasks error", err) })
       }).then(() => tasks)
  }


  update(user: User, fields: Array<string>, contact?: boolean): Promise<User> {
    var argv = { user: {  } }
    for (let field of fields)
      argv.user[field] = user[field]

    return new Promise<User>(resolve => {
        var req = new Req(`/mp/users/${
              contact ? user.id : "mine"}.json`, "PUT", argv)
        AjaxService.request(req).then(res => {
          if (!res.error)
            resolve()
        }).catch((err: any) => { console.log(err) })
      }).then(() => user)
  }
}
